package com.jinhua.medical.manager.util;

import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5加密
 * Created by 王兴光 on 2018/7/5.
 */
public class MD5Generator {
    private static Logger log = Logger.getLogger(MD5Generator.class);
    /**利用MD5进行加密
     * @param str  待加密的字符串
     * @return  加密后的字符串
     * @throws NoSuchAlgorithmException  没有这种产生消息摘要的算法
     * @throws UnsupportedEncodingException
     */
     public static String EncoderByMd5(String str) {
        //确定计算方法
         MessageDigest md5= null;
         try {
             md5 = MessageDigest.getInstance("MD5");
         } catch (NoSuchAlgorithmException e) {
             log.debug("MD5:没有这种产生消息摘要的算法");
             e.printStackTrace();
         }
         BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
         String newstr= null;
         try {
             newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
         } catch (UnsupportedEncodingException e) {
             log.debug("MD5:编码异常");
             e.printStackTrace();
         }
         return newstr;
    }

}
